package com.xc.hotelmgr.webapi.modules.op.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.xc.hotelmgr.webapi.common.annotation.SysLog;
import com.xc.hotelmgr.webapi.modules.dt.service.StoreService;
import com.xc.hotelmgr.webapi.modules.op.cache.HtlPriceCache;
import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xc.hotelmgr.webapi.modules.op.entity.HtlPriceEntity;
import com.xc.hotelmgr.webapi.modules.op.service.HtlPriceService;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;



/**
 * 
 *
 * @author kemmy
 * @email kemmy@gmail.com
 * @date 2020-09-29 14:30:31
 */
@RestController
@RequestMapping("op/htlprice")
@RequiredArgsConstructor
public class HtlPriceController extends AbstractController {

    private final HtlPriceService htlPriceService;

    private final HtlPriceCache htlPriceCache;

    private final StoreService storeService;


    /**
     * 列表
     */
    @SysLog
    @RequestMapping("/list")
    @RequiresPermissions("op:htlprice:list")
    public R list(@RequestParam Map<String, Object> params){
        String dtId = getUserDtId().toString();
        params.put("dtId",dtId);
        PageUtils page = htlPriceService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @SysLog
    @RequestMapping("/info/{id}")
    @RequiresPermissions("op:htlprice:info")
    public R info(@PathVariable("id") Integer id){
		HtlPriceEntity htlPrice = htlPriceService.getById(id);

        return R.ok().put("htlPrice", htlPrice);
    }

    /**
     * 保存
     */
    @SysLog
    @RequestMapping("/save")
    @RequiresPermissions("op:htlprice:save")
    public R save(@RequestBody HtlPriceEntity htlPrice){
        Integer dtId = getUserDtId();
        htlPrice.setDtId(dtId);
        Long hotelId = htlPrice.getHotelId();
        Integer storeId = htlPrice.getStoreId();
		htlPriceService.save(htlPrice);
		boolean result = priceCacheInit(storeId, hotelId);
		if (!result) {
		    return R.error("酒店加价保存失败");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog
    @RequestMapping("/update")
    @RequiresPermissions("op:htlprice:update")
    public R update(@RequestBody HtlPriceEntity htlPrice){
		htlPriceService.updateById(htlPrice);
        Long hotelId = htlPrice.getHotelId();
        Integer storeId = htlPrice.getStoreId();
        boolean result = priceCacheInit(storeId, hotelId);
        if (!result) {
            return R.error("酒店加价缓存更新失败");
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog
    @RequestMapping("/delete")
    @RequiresPermissions("op:htlprice:delete")
    public R delete(@RequestBody Map<String, Object> params){
        Integer id = (Integer) params.get("id");
        Integer storeId = (Integer) params.get("storeId");
        Long hotelId = Long.valueOf(params.get("hotelId").toString());
		htlPriceService.removeById(id);
        boolean result = priceCacheInit(storeId, hotelId);
        if (!result) {
            return R.error("酒店加价缓存更新失败");
        }
        return R.ok();
    }


    /**
     * 酒店加价缓存更新或增加
     * @param storeId
     * @param hotelId
     * @return
     */
    private boolean priceCacheInit(Integer storeId, Long hotelId) {
        try {
            String appKey = storeService.getById(storeId).getAppkey();
            List<HtlPriceEntity> htlPriceEntities = htlPriceService.getListByHotelId(hotelId);
            Map<String, Object> map = new HashMap<>();
            map.put(hotelId.toString(), JSONObject.toJSONString(htlPriceEntities));
            htlPriceCache.addOrUpdate(appKey, map);
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return false;
        }
        return true;
    }

}
